package simbot.example.dto;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import simbot.example.entity.BangumiEntity;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.DayOfWeek;
import java.util.LinkedList;
import java.util.List;

/**
 * The type Bangumi dto.
 *
 * @author minmin
 * @since 2021 /07/10 12:12
 */
@Getter
public class BangumiDTO implements Serializable {
    private final String weekday;
    private final List<Item> items;

    /**
     * Instantiates a new Bangumi dto.
     *
     * @param bangumiEntity the bangumi entity
     * @param dayOfWeek     the day of week
     * @throws InvocationTargetException the invocation target exception
     * @throws NoSuchMethodException     the no such method exception
     * @throws IllegalAccessException    the illegal access exception
     */
    public BangumiDTO(BangumiEntity[] bangumiEntity, DayOfWeek dayOfWeek) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        this(bangumiEntity, dayOfWeek, ImageClarity.common);
    }

    /**
     * Instantiates a new Bangumi dto.
     *
     * @param bangumiEntity the bangumi entity
     * @param dayOfWeek     the day of week
     * @param imageClarity  the image clarity
     * @throws InvocationTargetException the invocation target exception
     * @throws NoSuchMethodException     the no such method exception
     * @throws IllegalAccessException    the illegal access exception
     */
    public BangumiDTO(BangumiEntity[] bangumiEntity, DayOfWeek dayOfWeek, ImageClarity imageClarity) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        int ordinal = dayOfWeek.ordinal();
        BangumiEntity bangumiItem = bangumiEntity[ordinal];
        weekday = bangumiItem.getWeekday().getCn();
        items = new LinkedList<>();
        for (BangumiEntity.Items bangumiItemItem : bangumiItem.getItems()) {
            Item item = new Item();
            BeanUtils.copyProperties(bangumiItemItem, item);
            item.image = imageClarity.getValue(bangumiItemItem.getImages());
            items.add(item);
        }
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder("今天是 " + weekday + " 今日放送的番剧：");
        for (Item item : items) {
            result.append("\n").append(item.toString());
        }
        return result.toString();
    }

    /**
     * The enum Image clarity.
     */
    public enum ImageClarity {
        /**
         * Large image clarity.
         */
        large("Large"),
        /**
         * Common image clarity.
         */
        common("Common"),
        /**
         * Medium image clarity.
         */
        medium("Medium"),
        /**
         * Small image clarity.
         */
        small("Small"),
        /**
         * Grid image clarity.
         */
        grid("Grid");

        private final String key;

        ImageClarity(String key) {
            this.key = key;
        }

        /**
         * Gets value.
         *
         * @param images the images
         * @return the value
         * @throws NoSuchMethodException     the no such method exception
         * @throws InvocationTargetException the invocation target exception
         * @throws IllegalAccessException    the illegal access exception
         */
        public String getValue(BangumiEntity.Items.Images images) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
            if (StringUtils.isEmpty(images)) {
                return "";
            }
            Method method = images.getClass().getMethod("get" + key);
            return (String) method.invoke(images);
        }
    }

    @Getter
    @Setter
    public static class Item {
        private Integer id;
        private String url;
        private Integer type;
        private String name;
        private String nameCn;
        private String summary;
        private String airDate;
        private Integer airWeekday;
        private String image;
        private BangumiEntity.Items.Collection collection;
        private BangumiEntity.Items.Rating rating;
        private Integer rank;

        @Override
        public String toString() {
            return name
                    + "(" + nameCn + ")  首播:"
                    + airDate + "\n"
                    + image;
        }
    }
}
